package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.dto.DeleteDTO;
import com.example.mybatisplus.model.dto.PageDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.WhitelistSettingService;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 *
 *  前端控制器
 *
 * @author lxp
 * @since 2024-07-10
 * @version v1.0
 */
@RestController
@RequestMapping("/api/whitelistSetting")
public class WhitelistSettingController {

    @Autowired
    private WhitelistSettingService whitelistSettingService;

    @PostMapping("login")
    public JsonResponse login(@RequestBody WhitelistSetting whitelistSetting){
        WhitelistSetting user =whitelistSettingService.login(whitelistSetting);
        return JsonResponse.success(user);
    }

    @GetMapping("logout")
    public JsonResponse logout(){
        SessionUtils.clearCurrentUser();
        return JsonResponse.success(true);
    }

    // 这个是搞实验，先搞着看看哈
    // 这就是控制层，只需要这样子写
    @GetMapping("pageList")
    public JsonResponse pageList(WhitelistSetting whitelistSetting, PageDTO dto){
       Page<WhitelistSetting> page = whitelistSettingService.pageList(whitelistSetting,dto);
       return JsonResponse.success(page);
    }

    @GetMapping("removeById")
    public JsonResponse removeById(Long id){
        boolean b = whitelistSettingService.removeById(id);
        return JsonResponse.success(b);
    }

    @PostMapping("removeByIds")
    public JsonResponse removeByIds(@RequestBody DeleteDTO dto){
        boolean b = whitelistSettingService.removeByIds(dto.getIds());
        return JsonResponse.success(b);
    }

    // 修改用户
    @PostMapping("edit")
    public JsonResponse edit(@RequestBody WhitelistSetting whitelistSetting){
        boolean b = whitelistSettingService.updateById(whitelistSetting);
        return JsonResponse.success(b);
    }

    // 数据导出
    // 1.必须是post请求 2.参数中必须有响应 3.不需要返回
    @PostMapping("exportUser")
    public void exportUser (HttpServletResponse response)  {
            whitelistSettingService.exportUser(response);
    }

    @PostMapping(value = "/importUser")
    public JsonResponse upload(MultipartFile file) throws IOException{
        whitelistSettingService.importUser(file);
        return JsonResponse.success(true);
    }
}

